package com.ruoyi.office.service.impl;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.biz.bean.bo.CaseInfoIdNameBO;
import com.ruoyi.biz.domain.diskfile.FileInfo;
import com.ruoyi.biz.service.caseinfo.ICaseInfoService;
import com.ruoyi.biz.service.diskfile.IFileItemStoreService;
import com.ruoyi.comm.crud.bean.helper.BeanDTOUtils;
import com.ruoyi.comm.crud.service.impl.BaseBQImpls;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.page.PageQO;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanCopier;
import com.ruoyi.office.bean.bo.ChopInfoBO;
import com.ruoyi.office.bean.vo.dto.ChopInfoDTO;
import com.ruoyi.office.bean.vo.qo.ChopInfoQO;
import com.ruoyi.office.domain.ChopInfo;
import com.ruoyi.office.domain.ChopType;
import com.ruoyi.office.mapper.ChopInfoMapper;
import com.ruoyi.office.service.IChopInfoRefuseService;
import com.ruoyi.office.service.IChopTypeService;

/**
 * 案件信息 服务层处理
 * 
 * @author riverbo
 */
@Service
public class ChopInfoRefuseServiceImpl extends BaseBQImpls<ChopInfoMapper, ChopInfo, ChopInfoBO, ChopInfoQO> implements IChopInfoRefuseService {

	@Autowired
	private IChopTypeService chopTypeService;

	@Autowired
	private ICaseInfoService caseInfoService;
	
	@Autowired
	private IFileItemStoreService storeService;

	@Override
	public Boolean checkNameUnique(ChopInfo t) {
//        Long boId = StringUtils.isNull(t.getCaseTypeId()) ? -1L : t.getCaseTypeId();
//        Long dbId = this.baseMapper.getIdByName(t);
//        if (StringUtils.isNotNull(dbId) && dbId.longValue() != boId.longValue()) {
//            return UserConstants.NOT_UNIQUE;
//        }
        return UserConstants.UNIQUE;
	}
	
	@Override
	public List<ChopInfoBO> queryBO(ChopInfoQO q, PageQO page) {
		List<ChopInfoBO> list = super.queryBO(q, page);
		Map<Integer, ChopType> mapChopType = chopTypeService.mapAll();
		for (ChopInfoBO u : list) {
			Integer ctId = u.getChopTypeId();
			if (ctId != null) {
				ChopType bean = mapChopType.get(ctId);
				if (bean != null) {
					u.setChopTypeName(bean.getChopTypeName());
				}
			}
		}
		return list;
	}
		
	public ChopInfoBO selectBOById(Long id) {
		ChopInfoBO u = super.selectBOById(id);
		//
		String caseNo = u.getCaseNo();
		if (!StringUtils.isEmpty(caseNo)) {
			CaseInfoIdNameBO caseInfo = caseInfoService.selectByCode(caseNo, CaseInfoIdNameBO.class);
			u.setCaseInfo(caseInfo);
		}
		//if (!u.getStatus().equals("0")) {
		readAttachById(u);
		//}
//		String caseNo = u.getCaseNo();
//		if (!StringUtils.isEmpty(caseNo)) {
//		}
//		Map<Integer, CaseType> mapCaseType = caseTypeService.mapAll();
//		Map<Integer, LitigateType> mapLt = litigateTypeService.mapAll();
//		Integer ctId = u.getChopTypeId();
//		if (ctId != null) {
//			CaseType bean = mapCaseType.get(ctId);
//			if (bean != null) {
//				u.setCaseTypeName(bean.getCaseTypeName());
//			}
//		}
		return u;
	}
	
	@Override
	public void readAttachById(ChopInfoBO u) {
		Integer chopId = u.getChopId();
	    String sub = String.format("chopinfo/%d", chopId);
	    Stream<Path> ll = storeService.loadAll(sub);
	    List<FileInfo> fileInfos = new ArrayList<>();
	    if (ll != null) {
		    fileInfos = ll.map(path -> {
		      String filename = path.getFileName().toString();
		      String url = "";//MvcUriComponentsBuilder
		          //.fromMethodName(FileItemController.class, "getFile", path.getFileName().toString()).build().toString();
		      return new FileInfo(filename, url);
		    }).collect(Collectors.toList());
	    }
	    u.setFiles(fileInfos);
	}
	
	@Override
	public int insertA(Object a) {
		ChopInfoDTO dto = (ChopInfoDTO)a;
		ChopInfo bean = BeanCopier.copyProperties(a, this.getEntityClass());
		int n = super.insert(bean);
		if ( n>0 ) {
			int id = bean.getChopId();
			dto.setChopId(id);
		}
		return n;
	}
		
	@Override
	public <A> int updateA(A a) {
		ChopInfo bean = BeanCopier.copyProperties(a, this.getEntityClass());
		int n = super.updateA(bean);
		return n;
	}
	
	@Override
	public <A> int applyA(A a) {
		ChopInfoDTO dto = (ChopInfoDTO)a;
    	ChopInfo store = super.selectById(dto.getChopId());
    	if (store == null) {
    		return 0;
    	}
    	if (!StringUtils.equals(store.getStatus(), "3")) {
    		return 0;
    	}
    	ChopInfo saveBean = new ChopInfo();
    	BeanDTOUtils.copyUpdate(a, saveBean);
    	saveBean.setChopId(dto.getChopId());
    	saveBean.setStatus("1");
    	return this.baseMapper.update(saveBean);
	}

}
